package com.glasses.manage.auth.controller;

import com.glasses.manage.auth.dao.AccountDao;
import com.glasses.manage.auth.entity.Account;
import com.glasses.manage.auth.entity.Words;
import com.glasses.manage.auth.service.AccountService;
import com.glasses.manage.auth.service.BECTest2Service;
import com.glasses.manage.auth.service.BECTestService;
import com.glasses.manage.core.entity.PageBean;
import com.glasses.manage.core.ezpage.EzPageParam;
import com.glasses.manage.core.ezpage.PageUtils;
import com.glasses.manage.utils.JSONUtil;
import com.glasses.manage.utils.MessageBox;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @Auther: yuyang
 * @Date: 2019/7/1 15:15
 * @ClassName:BECTestController
 * @Description: TODO
 */
@RequestMapping(value = {"/account"})
@Controller
public class AccountController {


    @Autowired
    private AccountService accountService;

    @Autowired
    private AccountDao accountDao;



    /**
     * 跳转bec页面
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String manage_index() {
        return "account";
    }


    /**
     * 查询出所有的单词
     * @param pageParam
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getBecList(EzPageParam pageParam) throws Exception{
        PageBean<Account> accounts=new PageBean<>();
        accounts = accountService.getAll(PageUtils.convert(pageParam));
        return JSONUtil.object2Json(PageUtils.convertPageBean(accounts));
    }


    /**
     * 获取一条选择题
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/selectTest", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getBecTest() throws Exception{
        try{
            List<Account> question =accountDao.getOne();
            return JSONUtil.object2Json(question);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 重置选择的表的所有数据
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/reset", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String reset() throws Exception{
        Account account=new Account();
        account.setState(0);
        accountDao.update1(account);

        return JSONUtil.object2Json(MessageBox.getSuccessMsg("重置成功"));
    }



    /**
     * 校验单词测试正确性
     * @param questId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/check1", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String check1(Long questId) throws Exception{
        Account account=new Account();
        account.setId(questId);
        account.setState(1);
        accountDao.update1(account);
        return JSONUtil.object2Json(account);
    }

    /**
     * 导入excel
     * 采用file.Transto 来保存上传的文件
     *
     * @return
     */
    @RequestMapping("/importExcel")
    @ResponseBody
    public MessageBox importexcel(MultipartFile planFile) throws Exception {
        String str = null;
        //通过MultipartFile的方法直接写文件
        str = accountService.importExcel(planFile.getInputStream());
        if (str.equals("success")) {
            return MessageBox.getSuccessMsg("导入成功");
        } else {
            return MessageBox.getWarningMsg(str);
        }
    }


}
